import 'package:cat_eye/config/color_config.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';

/*
* @Package: util
* @ClassName: util
* @Author: szj
* @CreateDate: 7/28/21 3:36 PM
*/

/*
 * 顶部appBar封装
 */
Widget appBar(String title, {Color backgroundColor = ColorConfig.white}) {
  return AppBar(
    //标题居中
    centerTitle: true,
    //阴影高度
    elevation: 0,
    title: Text(
      title,
      style: TextStyle(
        color: Colors.black,
      ),
    ),
    backgroundColor: backgroundColor,
  );
}

/*
 *  跳转页面 widget 需要跳转的页面
 */
PageRouteBuilder router(Widget widget) {
  return PageRouteBuilder(
    //设置平移时间
    transitionDuration: Duration(milliseconds: 250),
    pageBuilder: (BuildContext context, Animation<double> animation,
        Animation<double> secondaryAnimation) {
      //FadeTransition 渐变动画
      return FadeTransition(
        opacity: animation,
        //SlideTransition 平移动画
        child: SlideTransition(
          position: Tween(begin: Offset(1.0, 0.0), end: Offset(0.0, 0.0))
              .animate(animation),
          child: widget,
        ),
      );
    },
  );
}

/*
 * 底部提醒 flutter没有自带Toast 所以就用的这个
 */
void snackBar(BuildContext context, String title) {
  var snackBar = SnackBar(
    content: Text(title),
    duration: Duration(seconds: 1),
  );
  Scaffold.of(context).showSnackBar(snackBar);
}

/*
 *   网络图片缓存  start
 */
Widget cacheImage(String url,
    {double width,
      double height,
      fit: BoxFit.cover,
      BorderRadius borderRadius,
      BoxShape shape = BoxShape.rectangle,
      bool clearMemoryCacheWhenDispose = false}) {
  return ExtendedImage.network(
    url,
    shape: shape,
    height: height,
    width: width,
    fit: fit,
    borderRadius: borderRadius,
    //图片从 tree 中移除，清掉内存缓存，以减少内存压力
    clearMemoryCacheWhenDispose: clearMemoryCacheWhenDispose,
  );
}

/*
 * 参考文档 [https://github.com/fluttercandies/extended_image/blob/master/README-ZH.md]
 */
ImageProvider cachedNetworkImageProvider(String url){
  return ExtendedNetworkImageProvider(url);
}
// ========  stop ===========
